package cn.zhb.backend.core.entity;

//import cn.zhb.backend.handler.LocalDateTimeJpaConverter;
//import cn.zhb.backend.handler.LocalDateTimeTypeHandler;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import javax.persistence.*;
import java.io.Serializable;
import java.time.LocalDateTime;

/***
 *@Description
 *@Author ZHB
 *@Create 2025/5/10 下午1:31
 **/
@Data
@NoArgsConstructor
@SuperBuilder
@MappedSuperclass // JPA注解，表示这是父类不会被映射为单独的表
public abstract class BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY) //JPA主键生成策略
    @TableId(value = "id", type = IdType.AUTO) // MyBatis-Plus主键策略 保持与JPA一致
    private Long id;

//    @Convert(converter = LocalDateTimeJpaConverter.class)
    @Column(name = "create_time", updatable = false)  // JPA列定义
//    @TableField(value = "create_time", fill = FieldFill.INSERT, typeHandler = LocalDateTimeTypeHandler.class)
    @TableField(value = "create_time", fill = FieldFill.INSERT) // MP自动填充
    private LocalDateTime createTime;


//    @Convert(converter = LocalDateTimeJpaConverter.class)
    @Column(name = "update_time")  // JPA列定义
//    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE, typeHandler = LocalDateTimeTypeHandler.class)
    @TableField(value = "update_time", fill = FieldFill.INSERT_UPDATE)  // MP自动填充
    private LocalDateTime updateTime;

    // JPA持久化前的回调方法
    @PrePersist
    protected void onCreate() {
        this.createTime = LocalDateTime.now();
        this.updateTime = LocalDateTime.now();
    }

    // JPA更新前的回调方法
    @PreUpdate
    protected void onUpdate() {
        this.updateTime = LocalDateTime.now();
    }

}
